package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       return roleService.findPage(queryPageBean);
    }


    /**
     * 查询所有角色权限
     * @param
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Permission> permissionList=roleService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }

 /**
     * 根据id查询角色权限
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByCheckRoleId")
    public Result findPermissionIdsByCheckRoleId(Integer id){
        try {
            List<Integer> permissionIds=roleService.findPermissionIdsByCheckRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }

    /**
     *添加权限
     * @param
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds,Integer roleId){
        try {
            roleService.add(permissionIds,roleId);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }

    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer roleId){
        try {
            roleService.delete(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }

    }

    //查询所有角色
    @RequestMapping("/findRole")
    public Result findRole(){
        try{
            List<Role> roleList = roleService.findRole();
            return new Result(true,"查询角色成功",roleList);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询角色失败");//查询失败
        }
    }



}
